package com.zhanghe.study.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 获取Cookie
 */
public class GetCookieServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Cookie[] cookies = req.getCookies();
        // 输出到客户端使用UTF-8编码文字
        resp.setCharacterEncoding("UTF-8");
        // 指定浏览器看该数据时使用什么编码看
        resp.setHeader("Content-Type","text/html;charset=utf-8");
        for(Cookie cookie : cookies){
            System.out.println(cookie.getValue());
            resp.getWriter().println(cookie.getName()+"----"+cookie.getValue());
        }
    }
}
